How to format strings in python, javascript and go?

How to Format strings in Python, JavaScript and GO

Python

Python provides a powerful and flexible way to format strings using the str.format() method or f-strings.

Example 1:

name = "Alice"

age = 25

print("My name is {} and I am {} years old.".format(name, age))

 

Output:

My name is Alice and I am 25 years old.

 

 

Example 2:

name = "Bob"

age = 30

print(f"My name is {name} and I am {age} years old.")

 

Output:

My name is Bob and I am 30 years old.

 

Example 3:

num1 = 10

num2 = 20

result = num1 * num2

print("The product of {} and {} is {}".format(num1, num2, result))

 

Output:

The product of 10 and 20 is 200

 

Example 4:

name = "Alice"

age = 25

print("My name is {0} and I am {1} years old. {0} is a nice name.".format(name, age))

 

Output:

My name is Alice and I am 25 years old. Alice is a nice name.

 

JavaScript

JavaScript also provides different ways to format strings including template literals (backtick strings), concatenation, and String.format() method.

Example 1:

const name = "Alice";

const age = 25;

console.log(`My name is ${name} and I am ${age} years old.`);

 

Output:

My name is Alice and I am 25 years old.

 

Example 2:

const name = "Bob";

const age = 30;

console.log("My name is " + name + " and I am " + age + " years old.");

 

Output:

My name is Bob and I am 30 years old.

 

Example 3:

const num1 = 10;

const num2 = 20;

const result = num1 * num2;

console.log(`The product of ${num1} and ${num2} is ${result}`);

 

Output:

The product of 10 and 20 is 200

 

 

Example 4:

const name = "Alice";

const age = 25;

console.log(`My name is ${name} and I am ${age} years old. ${name} is a nice name.`);

 

Output:

My name is Alice and I am 25 years old. Alice is a nice name.

 

 

Go

Go also provides a way to format strings using the fmt package.

 

Example 1:

package main

 

import "fmt"

 

func main() {

    name := "Alice"

    age := 25

    fmt.Printf("My name is %s and I am %d years old.\n", name, age)

}

 Output:

My name is Alice and I am 25 years old.

 

 

Example 2:

package main

 

import "fmt"

 

func main() {

    name := "Bob"

    age := 30

    fmt.Printf("My name is %s and I am %d years old.\n", name, age)

}

 

Output:

My name is Bob and I am 30 years old.

 

 

Example 3:

package main

 

import "fmt"

 

func main() {

    num1 := 10

    num2 := 20

    result := num1 * num2

    fmt.Printf("The product of %d and %d is %d\n", num1, num2, result)

}

Output:

The product of 10 and 20 is 200

 

 

Example 4:

package main

 

import "fmt"

 

func main() {

    name := "Alice"

    age := 25

    fmt.Printf("My name is %s and I am %d years old. %s is a nice name.\n", name, age, name)

}

Output:

My name is Alice and I am 25 years old. Alice is a nice name.

Comments

Popular posts from this blog

Data Science Essentials: Mastering Basic Statistics for Effective Analysis

What is a docker hub?

Explain Docker run with options